<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 10:32
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Order as OrderModel;
use app\api\model\OrderAfterSale;
use app\api\model\OrderFigureMeasureData as OrderFigureMeasureDataModel;
use app\api\model\OrderAfterSale as OrderAfterSaleModel;
use app\api\service\OrderAfterSale as OrderAfterSaleService;
use app\api\validate\ExpressValidate;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\OrderAftermarketValidate;
use app\api\validate\OrderValidate;
use app\lib\exception\OrderException;
use app\lib\exception\OrderFigureMeasureDataException;
use app\lib\exception\SuccessMessage;
use think\Controller;

class Order extends BaseController {

    /**
     * 获取用户定制订单列表
     * 2018-1-17 14:38 支持量体师获取
     * @param int $status
     * @param int $comment_status
     * @param int $is_pattern_master
     * @return array|\think\Paginator
     */
    public function getMemberOrderList($status = 0, $comment_status = 0, $is_pattern_master = 0) {

        // 可选参数，如果传了就要验证
        if($status > 0) (new OrderValidate)->goCheck('status');

        if($comment_status > 0) (new OrderValidate)->goCheck('comment_status');

        $orders = OrderModel::getMemberOrderList($status, $comment_status, $is_pattern_master);
//        dump($orders);
//        $data = (object)array();
//        $data->aaa = 'bbb';
        $orders = $orders->toArray();
//        dump($orders);
        return $orders;
    }

    /**
     * 确认定制订单数据
     * @param $id
     * @param string $attr
     * @return array
     * @throws OrderException
     */
    public function bookingQuantityBody($id, $attr = '') {
        (new IDMustBePostiveInt())->goCheck();

        $order = OrderModel::bookingQuantityBody($id, $attr);

        if (!$order) {
            throw new OrderException();
        }

        return $order;
    }

    /**
     * 创建定制订单
     * @return array|mixed
     */
    public function createOrder() {
        (new IDMustBePostiveInt())->goCheck();

        $OrderValidate = new OrderValidate();
        $OrderValidate->goCheck('create_order');
        $params = $OrderValidate->getDataByRule(input('post.'));
        $params['id'] = input('param.id');
        $order = OrderModel::createOrder($params);
        return $order;
    }

    /**
     * 获取定制订单详情，支持量体师获取
     * @param $id
     * @param int $is_pattern_master
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws OrderException
     */
    public function getOrderInfo($id, $is_pattern_master = 0) {
        (new IDMustBePostiveInt())->goCheck();

        $order = OrderModel::getOrderInfo($id, $is_pattern_master);

        $order = $order->toArray();

        return $order;
    }

    /**
     * 根据ID删除定制订单
     * @return int
     * @throws OrderException
     */
    public function deleteOrder() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $order = OrderModel::deleteOrder($id);
        if (!$order) {
            throw new OrderException([
                'msg' => '订单删除失败',
                'errorCode' => 60003
            ]);
        }
        return json(new SuccessMessage(), 201);
    }


    /**
     * 确认收货
     * @return \think\response\Json
     * @throws OrderException
     */
    public function orderConfirmReceipt() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $order = OrderModel::orderConfirmReceipt($id);
        if (!$order) {
            throw new OrderException([
                'msg' => '确认收货失败',
                'errorCode' => 60004
            ]);
        }
        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取订单量体数据, 支持量体师获取
     * @param $id
     * @param int $is_pattern_master
     * @return null|\think\Model
     * @throws OrderFigureMeasureDataException
     */
    public function figureMeasure($id, $is_pattern_master = 0) {
        (new IDMustBePostiveInt())->goCheck();
        $figure_measure_data = OrderFigureMeasureDataModel::getFigureMeasureByOrderId($id, $is_pattern_master);
        if (!$figure_measure_data) {
            throw new OrderFigureMeasureDataException();
        }
        return $figure_measure_data;
    }

    /**
     * 获取申请售后页面数据
     * @param $id
     * @return array
     */
    public function applyAftermarketData($id) {
        (new IDMustBePostiveInt())->goCheck();
        $data = OrderAfterSaleModel::applyAftermarketData($id);
        return $data;
    }

    /**
     * 创建售后单
     * @return \think\response\Json
     */
    public function createAftermarket() {
        $orderAftermarketValidate = new OrderAftermarketValidate();
        $orderAftermarketValidate->goCheck('create');
        $params = $orderAftermarketValidate->getDataByRule(input('post.'));

        if (input('?post.image')) {
            $image = input('post.image/a');
            $params['image'] = $image;
        }

        OrderAfterSaleService::createAftermarket($params);

        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取定制订单售后列表
     * @return \think\Paginator
     */
    public function afterSaleList() {
        $after_sale_list = OrderAfterSaleModel::afterSaleList()->toArray();
        return $after_sale_list;
    }

    /**
     * 获取售后单详情
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws OrderException
     */
    public function afterSaleInfo($id) {
        (new IDMustBePostiveInt())->goCheck();
        $data = OrderAfterSaleModel::afterSaleInfo($id);
        if (!$data) {
            throw new OrderException([
                'meg' => '售后单不存在',
                'errorCode' => 60007
            ]);
        }
        return $data;
    }

    /**
     * 用户保存售后单物流信息
     * @return \think\response\Json
     */
    public function saveLogistics() {
        (new OrderAftermarketValidate())->goCheck('logistics');
        $id = input('param.id');
        $member_express_sn = input('param.member_express_sn');
        OrderAfterSaleModel::saveLogistics($id, $member_express_sn);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 售后单确认收货
     * @return \think\response\Json
     */
    public function afterSaleConfirmReceipt(){
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        OrderAfterSaleModel::afterSaleConfirmReceipt($id);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 取消售后单
     * @return \think\response\Json
     */
    public function cancelAfterSale() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        OrderAfterSaleModel::cancelAfterSale($id);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取物流信息
     * @param $express_sn
     * @return array
     */
    public function expressInfo($express_sn) {
        (new ExpressValidate())->goCheck();
        $express_info = OrderModel::expressInfo($express_sn);
        return $express_info;
    }
}